<template>
  <div class="app-container">
    <el-row class="library-row">
      <el-col :span="4">
        <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
          <el-button slot="append" icon="el-icon-search" @click="searchBooks"></el-button>
        </el-input>
      </el-col>
      <el-col :span="3" :offset="17" class="add">
        <el-button @click="addBooks" type="primary" icon="el-icon-plus">添加图书分类</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      stripe
       @sort-change="sortChange"
      style="width: 100%"
      :default-sort="{prop: 'category_id', order: 'descending'}"
    >
      <el-table-column prop="category_id" label="# " width="100"></el-table-column>
      <el-table-column prop="category_name" label="分类名称"></el-table-column>
            <el-table-column  label="排序" width="100" align="center" sortable="custom">
        <template slot-scope="scope">
          <el-input align="center" v-model="scope.row.category_order" @blur="change(scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="editor(scope.row)">编辑</el-button>
          <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="total">
      <el-pagination
        @current-change="currentPage"
        background
        :page-size="20"
        layout="prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-row>

    <!-- 通知弹窗 -->
    <el-dialog :title="title" width="30%" :close-on-click-modal="false"  :visible.sync="dialogFormVisible">
      <el-form :model="formData" ref="formSave" :rules="rules">
        <el-form-item label="图书分类标题" prop="category_name">
          <el-input v-model="formData.category_name" placeholder="图书分类名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书分类序号" prop="category_order">
          <el-input v-model="formData.category_order" placeholder="图书分类序号" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="图书分类说明" prop="category_desc">
          <el-input v-model="formData.category_desc" type="textarea" placeholder="图书分类说明" :rows="8"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save('formSave')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategoryList,
  delCategoryList,
  addCategoryForm,
  editorCategoryForm,
  addCategoryFormEditor,
  sortData
} from "@/api/category";

export default {
  data() {
    return {
      title: "添加图书分类信息",
      search: "",
      total: 1,
      isEdit: false,
      form: {
        page: 1,
        page_size: 20,
        type:'FILE',
        category: "",
        sort: "category_order",
        order:null
      },
      formData: {
        category_name: "",
        category_desc: "",
        type:'FILE',
        category_order:null
      },
      rules: {
        category_name: [
          { required: true, message: "请填写分类标题", trigger: "blur" },
        ],
        category_desc: [
          { required: true, message: "请填写分类说明", trigger: "blur" },
        ],
        category_order: [
          { required: true, message: "请填写分类序号", trigger: "blur" },
        ],
      },
      id: null,
      dialogFormVisible: false, //新增弹窗
      list: null,
      listLoading: true,
      value: "",
      tableData: [],
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      let params=this.form
      getCategoryList(params,'book').then((response) => {
        console.log(response);
        this.total = response.total;
        this.tableData = response.data;

        this.listLoading = false;
      });
    },
    // 删除请求
    async delete() {
      let id = this.id;
      let del = await delCategoryList(id,'book');
      if (del.code == 1) {
        this.$message({
          type: "success",
          message: del.message,
        });
      } else {
        this.$message({
          type: "info",
          message: del.message,
        });
      }
      this.fetchData();
    },
    // 保存新增请求
    async saveData() {
      let data = this.formData;
      console.log(data)
      let res = await addCategoryForm(data,'book');
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 保存编辑请求
    async saveDataEditor() {
      let data = this.formData;
      let res = await addCategoryFormEditor(data, this.id,'book');
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        this.fetchData();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 获取编辑请求
    async editorData() {
      let id = this.id;
      let res = await editorCategoryForm('book',id);
      if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
        console.log(res)
        this.formData = res.data;
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 编辑排序请求
    async sort(){
      let data={
        id:this.id,
        order:this.category_order
      }
      let res=await sortData(data,'bookSort')
        if (res.code == 1) {
        this.$message({
          type: "success",
          message: res.message,
        });
     this.fetchData();
      } else {
        this.$message({
          type: "info",
          message: res.message,
        });
      }
    },
    // 搜索
    searchBooks() {
      (this.form.page = 1), (this.form.category = this.search);
      this.fetchData()
    },
    // 添加图书
    addBooks() {
      setTimeout(() => {
        console.log(this.$refs.formSave);
        this.$nextTick(function () {
          this.$refs.formSave.clearValidate();
        });
      }, 50);
      this.dialogFormVisible = true;
      this.title = "添加图书分类信息";
      this.formData = {
        category_name: "",
        category_desc: "",
        type:'FILE'
      }
    },
    // 编辑
    editor(row) {
      console.log(row)
      this.id = row.category_id;
      this.dialogFormVisible = true;
      this.title = "编辑图书分类信息";
      this.editorData();
    },
    // 删除
    del(row) {
      this.id = row.category_id;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 排序修改
    change(row){
      console.log(row)
      this.id=row.category_id
      this.category_order=row.category_order
      this.sort()
    },
       // 排序
    sortChange(val){
      let order=val.order
      if(order=='descending'){
        this.form.order='desc'
      }else if(order=='ascending'){
        this.form.order='asc'
      }else{
        this.form.order=null
      }
    this.fetchData()
    },
    currentPage(val) {
      this.form.page = val;
       this.fetchData()
    },
    // 保存数据
    save(formSave) {
      this.$refs[formSave].validate((valid) => {
        if (valid) {
          if (this.title == "添加图书分类信息") {
            this.saveData();
            console.log(1)
          } else {
            this.saveDataEditor();
             console.log(2)
          }
        } else {
         
          return false;
        }
      });
    },
  },
};
</script>
<style>
.library-row {
  padding: 15px 0;
}

.add {
  display: flex;
  justify-content: flex-end;
}
.el-dialog__body {
  border: 1px solid #ddd;
}
.total {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}
</style>